'use strict';

/*
 * @Description: 测试路由服务
 * @Author: Mr.Wen
 * @Version: v1.0.0
 * @Date: 2022-07-30 10:38:17
 * @LastEditors: Mr.Wen - 文先生
 * @LastEditTime: 2023-05-23 09:40:55
 */

const express = require('express');
const router = express.Router();

const fs = require('fs');

const { GetAdressIP } = require('@/utils/utils');
const { filesPath } = require('@/routes/configPath');

/**
 * 获取本机IP地址
 * method：GET
 * params：无
 */
router.get('/web/ip', (req, res) => {
  let resultData = {
    msg: '测试路由服务',
    ip: GetAdressIP(),
  };

  let json = {
    status: 0, // 0-操作成功，1-操作失败
    code: 200,
    message: '操作成功', // 登录成功/失败 操作成功 令牌失效等等
    req_url: req.originalUrl,
    api_url: '/web/ip',
    result: resultData, // 返回数据结果  无数据时，为 null
  };

  res.send(json);
  return;
});

/**
 * 获取音乐、电影信息
 * method: GET
 * params: type 音乐 0，电影 1
 */
router.get('/us', (req, res) => {
  const { type } = req.query;

  const resultData = fs.readFileSync(filesPath('test-data/test.json'), 'utf-8');
  const { music, movie } = JSON.parse(resultData);
  let result = [];

  switch (type) {
    case '0':
      result = music;
      break;
    case '1':
      result = movie;
      break;
    default:
      result = '参数有误';
  }

  let json = {
    status: 0,
    code: 200,
    message: '操作成功',
    req_url: req.originalUrl,
    api_url: '/us',
    result,
  };

  res.send(json);
  return;
});

/**
 * 获取克莱因蓝网站信息
 * method：get
 * params：none
 */
router.get('/website/info', (req, res) => {
  const resultData = fs.readFileSync(
    filesPath('test-data/WebsitInfo'),
    'utf-8'
  );

  let json = {
    status: 0,
    code: 200,
    message: '操作成功',
    req_url: req.originalUrl,
    api_url: '/website/info',
    result: JSON.parse(resultData),
  };

  res.send(json);
  return;
});

module.exports = router;
